﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1.entity
{
    public abstract class Entity
    {
        public int id;
        public abstract string Write();
        public abstract void Upd(string[] args);

        public string Show()
        {
            string s = this.GetType().Name;
            foreach (var f in this.GetType().GetFields().Reverse())
            {
                if (f.GetValue(this) != null && f.GetValue(this).ToString().Trim() != String.Empty)
                {
                    if (Data.types.Contains(f.FieldType))
                    {
                        Entity e = (Entity)f.GetValue(this);
                        s += " " + f.Name + "=" + e.id;
                    }
                    else
                    {
                        s += " " + f.Name + "=" + f.GetValue(this);
                    }
                }
            }
            return s;
        }

        public string Spravka()
        {
            string s = this.GetType().Name + ":";

            foreach (var f in this.GetType().GetFields().Reverse())
            {
                s += " " + f.Name;
            }
            return s;
        }
        public void Remove()
        {
            foreach (var f in this.GetType().GetFields().Reverse())
            {
                if (Data.types.Contains(f.FieldType) && f.GetValue(this) != null)
                {
                    Entity e = (Entity)f.GetValue(this);
                    e.GetType().GetField(this.GetType().Name.ToLower()).SetValue(e, null);
                }
            }
            Data.entity.Remove(this);
        }
    }
}
